# @Time    : 2020/10/14 13:21
# @Author  : Del Fung
# @File    : data_depot.py
# @Software: PyCharm
from pathdict import PathDict


class DataDepot(object):
    """
    数据仓库
    """

    def __init__(self):
        self.DEPOT_NAME = list()
        self.DEPOT = PathDict()

    def add_depots(self, depot_names: list):
        for name in depot_names:
            self.DEPOT_NAME.append(name)
            self.DEPOT[name] = dict()

    def add_depot(self, depot_name):
        self.DEPOT_NAME.append(depot_name)
        self.DEPOT[depot_name] = dict()

    def depot_push(self, depot_name, data):
        if depot_name in self.DEPOT_NAME:
            self.DEPOT[depot_name].update(data)

    def update(self, depot_name, path, data: list or dict):
        if depot_name in self.DEPOT_NAME:
            if isinstance(data, list):
                if path:
                    self.DEPOT[f'{depot_name}.{path}'].extend(data)
                else:
                    self.DEPOT[f'{depot_name}'].extend(data)
            elif isinstance(data, str):
                if path:
                    self.DEPOT[f'{depot_name}.{path}'].append(data)
                else:
                    self.DEPOT[f'{depot_name}'].append(data)
            elif isinstance(data, dict):
                if path:
                    self.DEPOT[f'{depot_name}.{path}'].update(data)
                else:
                    self.DEPOT[f'{depot_name}'].update(data)

    def clear_depot(self, depot_name):
        if depot_name in self.DEPOT_NAME:
            self.DEPOT.pop(depot_name)

    def depot_pop(self, depot_name, path, node):
        if depot_name in self.DEPOT_NAME:
            self.DEPOT[f'{depot_name}.{path}'].pop(node)

    def get(self, depot_name, path):
        return self.DEPOT[f'{depot_name}.{path}']
